<!--
Copyright 2009-2018 Sven Bader
Dieses Programm ist urheberrechtlich geschützt. Veränderungen an Quelltext oder
Grafiken sind nicht gestattet. 

Jegliche Verwendung ohne schriftliche Genehmigung des Autors wird strafrechtlich verfolgt!
!-->
<html>
<head>
    <title>Mousometer</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
        #bodyid {
            margin: 0;
            width: 132px;
            height: 99px;
            padding: 0;
            position: absolute;
            border-width: 0;
            /*	background-image: url(back_1.png);
                background-repeat:no-repeat; */
            font-size: 11px;
            font-family: Calibri, segoe UI;
        }

        a:link {
            color: #fff;
        }

        a:visited {
            color: #fff
        }

        a:hover {
            color: #fff
        }

        a:active {
            color: #fff
        }

        a {
            text-decoration: none
        }

        a:hover {
            text-decoration: underline
        }

        img {
            border-width: 0;
        }

        div {
            position: absolute;
            overflow: hidden;
            color: #fff;
            left: 2px;
        }

        #z1_label {
            top: 27px;
            left: 12px;
            font-weight: normal;
        }

        #wert {
            top: 27px;
            left: 64px;
        }

        #z2_label {
            top: 42px;
            left: 12px;
            font-weight: normal;
        }

        #spd {
            top: 42px;
            left: 64px;
        }

        #z3_label {
            top: 57px;
            left: 12px;
            font-weight: normal;
        }

        #topspd {
            top: 57px;
            left: 64px;
        }

        #resetbtn {
            top: 77px;
            left: 12px;
            font-size: 11px;
        }

        #bild {
            font-family: Arial;
            top: 79px;
            left: 12px;
            font-size: 9px;
            width: 65px;
            height: 11px;
        }

        #status {
            font-family: Arial;
            top: 77px;
            left: 81px;
            font-size: 9px;
            width: 44px;
            height: 15px;
            text-align: right;
            font-weight: bold;
        }

    </style>

    <script language="javascript1.2" type="text/javascript">

        var x2 = -1, y2 = 0, wert, manchmal = 0;
        var wert2 = wert, m, m2, spd, topspd = 0, monitor = .0, monitornum, theme, themenum, z1, z2, z3, z1num, z2num, z3num, mstr, wertHeute, mHeute, mHeutestr;
        var webupdate = 0, user = '', url = '', pw = '', webwert = 0;
        var f = .0;
        var fs = .0;
        var button1 = '<img src="button1.png" title="www.mousometer.de">';
        var button0 = '<img src="button0.png" title="www.mousometer.de">';
        labels = new Array("", "total:", "today:", "speed:", "Top:", "Orbits:");
        werte =  new Array("", "", "", "", "", "");
        //var zeit=0


        function urlpar(name) {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(url);
            if (results == null) return 0;
            else return results[1];
        }

        function tag() {
            var jetzt = new Date();
            return (jetzt.getFullYear() + '-' + (jetzt.getMonth() + 1) + '-' + jetzt.getDate());
        }
        function reset1f() {
            wert = 0;
            wert2 = 0;
            wertHeute = 0;
            System.Gadget.Settings.write("wert", wert);
            System.Gadget.Settings.write("wertHeute", wertHeute);
// document.getElementById("wert").firstChild.nodeValue = "0,00 m";

        }

        function reset2f() {
            topspd = 0;
// document.getElementById("topspd").firstChild.nodeValue = "0,00 km/h";
            System.Gadget.Settings.write("topspd", topspd);
        }

        function formatstr(fstr) {
            fstr = fstr.toFixed(2).toString().replace(".", ",")
            if (fstr.indexOf(",") > 6) {
                fstr = fstr.substr(0, fstr.indexOf(","));
                fstr = fstr.substr(0, 4) + "." + fstr.substr(4);
                fstr = fstr.substr(0, 1) + "." + fstr.substr(1);
            }
            else if (fstr.indexOf(",") > 5) {
                fstr = fstr.substr(0, fstr.indexOf(","));
                fstr = fstr.substr(0, 3) + "." + fstr.substr(3);
            }
            else if (fstr.indexOf(",") > 4)   fstr = fstr.substr(0, 2) + "." + fstr.substr(2);
            else if (fstr.indexOf(",") > 3)     fstr = fstr.substr(0, 1) + "." + fstr.substr(1);
            return fstr;
        }


        MD5 = function (e) {
            function h(a, b) {
                var c, d, e, f, g;
                e = a & 2147483648;
                f = b & 2147483648;
                c = a & 1073741824;
                d = b & 1073741824;
                g = (a & 1073741823) + (b & 1073741823);
                return c & d ? g ^ 2147483648 ^ e ^ f : c | d ? g & 1073741824 ? g ^ 3221225472 ^ e ^ f : g ^ 1073741824 ^ e ^ f : g ^ e ^ f
            }

            function k(a, b, c, d, e, f, g) {
                a = h(a, h(h(b & c | ~b & d, e), g));
                return h(a << f | a >>> 32 - f, b)
            }

            function l(a, b, c, d, e, f, g) {
                a = h(a, h(h(b & d | c & ~d, e), g));
                return h(a << f | a >>> 32 - f, b)
            }

            function m(a, b, d, c, e, f, g) {
                a = h(a, h(h(b ^ d ^ c, e), g));
                return h(a << f | a >>> 32 - f, b)
            }

            function n(a, b, d, c, e, f, g) {
                a = h(a, h(h(d ^ (b | ~c), e), g));
                return h(a << f | a >>> 32 - f, b)
            }

            function p(a) {
                var b = "",
                        d = "",
                        c;
                for (c = 0; 3 >= c; c++) d = a >>> 8 * c & 255, d = "0" + d.toString(16), b += d.substr(d.length - 2, 2);
                return b
            }

            var f = [],
                    q, r, s, t, a, b, c, d;
            e = function (a) {
                a = a.replace(/\r\n/g, "\n");
                for (var b = "", d = 0; d < a.length; d++) {
                    var c = a.charCodeAt(d);
                    128 > c ? b += String.fromCharCode(c) : (127 < c && 2048 > c ? b += String.fromCharCode(c >> 6 | 192) : (b += String.fromCharCode(c >> 12 | 224), b += String.fromCharCode(c >> 6 & 63 | 128)), b += String.fromCharCode(c & 63 | 128))
                }
                return b
            }(e);
            f = function (b) {
                var a, c = b.length;
                a = c + 8;
                for (var d = 16 * ((a - a % 64) / 64 + 1), e = Array(d - 1), f = 0, g = 0; g < c;) a = (g - g % 4) / 4, f = g % 4 * 8, e[a] |= b.charCodeAt(g) << f, g++;
                a = (g - g % 4) / 4;
                e[a] |= 128 << g % 4 * 8;
                e[d - 2] = c << 3;
                e[d - 1] = c >>> 29;
                return e
            }(e);
            a = 1732584193;
            b = 4023233417;
            c = 2562383102;
            d = 271733878;
            for (e = 0; e < f.length; e += 16) q = a, r = b, s = c, t = d, a = k(a, b, c, d, f[e + 0], 7, 3614090360), d = k(d, a, b, c, f[e + 1], 12, 3905402710), c = k(c, d, a, b, f[e + 2], 17, 606105819), b = k(b, c, d, a, f[e + 3], 22, 3250441966), a = k(a, b, c, d, f[e + 4], 7, 4118548399), d = k(d, a, b, c, f[e + 5], 12, 1200080426), c = k(c, d, a, b, f[e + 6], 17, 2821735955), b = k(b, c, d, a, f[e + 7], 22, 4249261313), a = k(a, b, c, d, f[e + 8], 7, 1770035416), d = k(d, a, b, c, f[e + 9], 12, 2336552879), c = k(c, d, a, b, f[e + 10], 17, 4294925233), b = k(b, c, d, a, f[e + 11], 22, 2304563134), a = k(a, b, c, d, f[e + 12], 7, 1804603682), d = k(d, a, b, c, f[e + 13], 12, 4254626195), c = k(c, d, a, b, f[e + 14], 17, 2792965006), b = k(b, c, d, a, f[e + 15], 22, 1236535329), a = l(a, b, c, d, f[e + 1], 5, 4129170786), d = l(d, a, b, c, f[e + 6], 9, 3225465664), c = l(c, d, a, b, f[e + 11], 14, 643717713), b = l(b, c, d, a, f[e + 0], 20, 3921069994), a = l(a, b, c, d, f[e + 5], 5, 3593408605), d = l(d, a, b, c, f[e + 10], 9, 38016083), c = l(c, d, a, b, f[e + 15], 14, 3634488961), b = l(b, c, d, a, f[e + 4], 20, 3889429448), a = l(a, b, c, d, f[e + 9], 5, 568446438), d = l(d, a, b, c, f[e + 14], 9, 3275163606), c = l(c, d, a, b, f[e + 3], 14, 4107603335), b = l(b, c, d, a, f[e + 8], 20, 1163531501), a = l(a, b, c, d, f[e + 13], 5, 2850285829), d = l(d, a, b, c, f[e + 2], 9, 4243563512), c = l(c, d, a, b, f[e + 7], 14, 1735328473), b = l(b, c, d, a, f[e + 12], 20, 2368359562), a = m(a, b, c, d, f[e + 5], 4, 4294588738), d = m(d, a, b, c, f[e + 8], 11, 2272392833), c = m(c, d, a, b, f[e + 11], 16, 1839030562), b = m(b, c, d, a, f[e + 14], 23, 4259657740), a = m(a, b, c, d, f[e + 1], 4, 2763975236), d = m(d, a, b, c, f[e + 4], 11, 1272893353), c = m(c, d, a, b, f[e + 7], 16, 4139469664), b = m(b, c, d, a, f[e + 10], 23, 3200236656), a = m(a, b, c, d, f[e + 13], 4, 681279174), d = m(d, a, b, c, f[e + 0], 11, 3936430074), c = m(c, d, a, b, f[e + 3], 16, 3572445317), b = m(b, c, d, a, f[e + 6], 23, 76029189), a = m(a, b, c, d, f[e + 9], 4, 3654602809), d = m(d, a, b, c, f[e + 12], 11, 3873151461), c = m(c, d, a, b, f[e + 15], 16, 530742520), b = m(b, c, d, a, f[e + 2], 23, 3299628645), a = n(a, b, c, d, f[e + 0], 6, 4096336452), d = n(d, a, b, c, f[e + 7], 10, 1126891415), c = n(c, d, a, b, f[e + 14], 15, 2878612391), b = n(b, c, d, a, f[e + 5], 21, 4237533241), a = n(a, b, c, d, f[e + 12], 6, 1700485571), d = n(d, a, b, c, f[e + 3], 10, 2399980690), c = n(c, d, a, b, f[e + 10], 15, 4293915773), b = n(b, c, d, a, f[e + 1], 21, 2240044497), a = n(a, b, c, d, f[e + 8], 6, 1873313359), d = n(d, a, b, c, f[e + 15], 10, 4264355552), c = n(c, d, a, b, f[e + 6], 15, 2734768916), b = n(b, c, d, a, f[e + 13], 21, 1309151649), a = n(a, b, c, d, f[e + 4], 6, 4149444226), d = n(d, a, b, c, f[e + 11], 10, 3174756917), c = n(c, d, a, b, f[e + 2], 15, 718787259), b = n(b, c, d, a, f[e + 9], 21, 3951481745), a = h(a, q), b = h(b, r), c = h(c, s), d = h(d, t);
            return (p(a) + p(b) + p(c) + p(d)).toLowerCase()
        };


        function Speichern() {
            System.Gadget.Settings.write("wert", wert);
            System.Gadget.Settings.write("wertHeute", wertHeute);
        }

        function Fillreq() {
            req = new XMLHttpRequest();
            req.open("POST", "https://www.mousometer.de/mm.php");
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            req.onreadystatechange = retrievedDatareq;
            var URI = "user=" + user + "&send=1&heute=" + wertHeute + "&monitor=" + monitor + "&screenx=" + screen.width + "&screeny=" + screen.height + "&ver=1.5&pw=" + MD5(pw) + "&wert=" + escape(wert);
            req.send(URI);
        }

        function retrievedDatareq() {
            if (req.readyState >= 4 && req.status == 200) {
                url = req.responseText;
                if (parseInt(urlpar('wert')) > wert) { //Onlinewert größer?
                    wert = parseInt(urlpar('wert'));
                    if (parseInt(urlpar('heute')) > wertHeute) wertHeute = parseInt(urlpar('heute')); //nur dann auch heute Wert abfragen (sonst bei Tagwechsel problematisch)
                }

                document.getElementById("status").innerHTML = '<img src="status_' + req.responseText.substring(0, 1) + '.png">';
            }
        }


        function updateval() {
            var posi = new Object()
            //var datum2 = new Date()
            //zeit3 = datum2.getTime();
            posi.x = window.event.screenX;
            posi.y = window.event.screenY;
            if (x2 == -1 | y2 == -1) {
                x2 = posi.x;
                y2 = posi.y;
            }
            else {
                webupdate++;
                if (webupdate == 1200) { //zwei minuten
                    webupdate = 0;
                    if (user != '' && pw != '') Fillreq();
                }
                x = posi.x;
                y = posi.y;
                if (Math.abs(x) > 5000) x = 0; //wegen bug
                if (Math.abs(y) > 5000) y = 0; //wegen bug
                spd = (Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2)) * 10) / fs * 0.036; // cm/sek -> km/h
                if (spd > topspd) {
                    topspd = spd;
                    System.Gadget.Settings.write("topspd", topspd);
                }
                wert += Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
                wertHeute += Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
                x2 = x;
                y2 = y;
                m = (wert / f);
                mHeute = (wertHeute / f);
                mstr = formatstr(m);
                mHeutestr = formatstr(mHeute);

                if ((m2 != m) || ( werte[3] != "0,00 km/h")) {
                    if (tag() != datum) { //tag gewechselt
                        wertHeute = 0;
                        datum = tag();
                        System.Gadget.Settings.write("datum", datum);
                        System.Gadget.Settings.write("wertHeute", wertHeute);
                    }
                    manchmal++;
                    if (manchmal > 75) { //alle 7,5 Sec INI speichern
                        manchmal = 0;
                        Speichern();
                    }
                    if (System.Gadget.Settings.read("units") == 2) {
                        werte[1] = (m * 0.000621371).toFixed(2).toString().replace(",", ".") + " miles";
                        werte[2] = (mHeute * 0.000621371).toFixed(2).toString().replace(",", ".")  + " miles";
                        werte[3] = (spd * 0.621371).toFixed(2).toString().replace(",", ".") + " MPH";
                        werte[4] = (topspd * 0.621371).toFixed(2).toString().replace(",", ".") + " MPH";
                        werte[5] = (m / 40075000).toFixed(6).toString().replace(",", ".");
                    } else {
                        werte[1] = mstr + " m";
                        werte[2] = mHeutestr + " m";
                        werte[3] = spd.toFixed(2).toString().replace(".", ",") + " km/h";
                        werte[4] = topspd.toFixed(2).toString().replace(".", ",") + " km/h";
                        werte[5] = (m / 40075000).toFixed(6).toString().replace(".", ",");
                    }
                    m2 = m;
                    document.getElementById("wert").firstChild.nodeValue = werte[z1];
                    document.getElementById("spd").firstChild.nodeValue = werte[z2];
                    document.getElementById("topspd").firstChild.nodeValue = werte[z3];
                }
            }
        }

        function readSettings() {
            user = System.Gadget.Settings.readString("user");
            pw = System.Gadget.Settings.readString("pw");
            if (System.Gadget.Settings.read("datum")) {
                datum = System.Gadget.Settings.read("datum");
            }
            else {
                datum = tag();
                System.Gadget.Settings.write("datum", datum);
            }

            if (System.Gadget.Settings.read("wertHeute")) {
                wertHeute = System.Gadget.Settings.read("wertHeute");
            }
            else {
                wertHeute = 0;
                System.Gadget.Settings.write("wertHeute", 0);
            }

            if (tag() != datum) { //tag gewechselt
                wertHeute = 0;
                System.Gadget.Settings.write("datum", datum);
                System.Gadget.Settings.write("wertHeute", wertHeute);
            }


            if (System.Gadget.Settings.read("wert")) {
                wert = System.Gadget.Settings.read("wert");
                wert2 = wert;
            }
            else {
                wert = 0;
                wert2 = 0;
                System.Gadget.Settings.write("wert", 0);
            }
            if (System.Gadget.Settings.read("topspd")) {
                topspd = System.Gadget.Settings.read("topspd");
            }
            else {
                topspd = 0;
                System.Gadget.Settings.write("topspd", 0);
            }
            if (System.Gadget.Settings.read("monitor")) {
                monitor = System.Gadget.Settings.read("monitor");
                monitornum = System.Gadget.Settings.read("monitornum");
                //FLOAT BUG!
                if (monitor > 70) monitor = monitor / 10.0;
            } else {
                System.Gadget.Settings.write("monitor", 24);
                System.Gadget.Settings.write("monitornum", 15);
                monitor = 24;
            }
            if (System.Gadget.Settings.read("theme")) {
                theme = System.Gadget.Settings.read("theme");
                themenum = System.Gadget.Settings.read("themenum");
            } else {
                System.Gadget.Settings.write("theme", 1);
                System.Gadget.Settings.write("themenum", 0);
                theme = 1;
            }

            if (System.Gadget.Settings.read("z1")) {
                z1 = System.Gadget.Settings.read("z1");
                z1num = System.Gadget.Settings.read("z1num");
            } else {
                System.Gadget.Settings.write("z1", 1);
                System.Gadget.Settings.write("z1num", 0);
                z1 = 1;
            }

            if (System.Gadget.Settings.read("z2")) {
                z2 = System.Gadget.Settings.read("z2");
                z2num = System.Gadget.Settings.read("z2num");
            } else {
                System.Gadget.Settings.write("z2", 2);
                System.Gadget.Settings.write("z2num", 1);
                z2 = 2;
            }

            if (System.Gadget.Settings.read("z3")) {
                z3 = System.Gadget.Settings.read("z3");
                z3num = System.Gadget.Settings.read("z3num");
            } else {
                System.Gadget.Settings.write("z3", 3);
                System.Gadget.Settings.write("z3num", 2);
                z3 = 3;
            }
            f = (Math.sqrt((screen.width * screen.width) + (screen.height * screen.height)) / (monitor * 0.0254));
            fs = (Math.sqrt((screen.width * screen.width) + (screen.height * screen.height)) / (monitor * 2.54));

            if (theme == 99 && user != '') { //EIGENES DESIGN
                                             //https nicht möglich! Der Server leitet aber weiter, das geht
                document.getElementById("baa").src = 'http://www.mousometer.de/photos/_' + user + '.png?zufall=' + Math.random();
            }
            else document.getElementById("baa").src = 'back_' + theme + '.png';
            document.getElementById("z1_label").firstChild.nodeValue = labels[z1];
            document.getElementById("z2_label").firstChild.nodeValue = labels[z2];
            document.getElementById("z3_label").firstChild.nodeValue = labels[z3];

            reset1 = System.Gadget.Settings.read("reset1");
            reset2 = System.Gadget.Settings.read("reset2");
            if (reset1 == true) {
                reset1 = false;
                System.Gadget.Settings.write("reset1", false);
                reset1f();
            }
            if (reset2 == true) {
                reset2 = false;
                System.Gadget.Settings.write("reset2", false);
                reset2f();
            }


        }
        function showopt() {
            System.Gadget.Flyout.show = (System.Gadget.Flyout.show) ? false : true;
        }

        function hideopt() {
            user = System.Gadget.Settings.readString("user");
            pw = System.Gadget.Settings.readString("pw");
            webupdate = 1199;  //Wert online speichern (nach evtl. Eingabe von Benutzername und PW)
        }

        function fillopt() {
            var optDiv = System.Gadget.Flyout.document;
            var txt = "Test";
//		  optDiv.getElementById("content").innerHTML = txt;
        }


        function init() {
            webupdate = 1199;
            System.Gadget.Flyout.file = "opt.html";
            System.Gadget.Flyout.onShow = fillopt;
            System.Gadget.Flyout.onHide = hideopt;
            // System.Gadget.visibilityChanged = hideopt; //hoffentlich auch beim beenden/herunterfahren onlinewert speichern!
            System.Gadget.settingsUI = "Setup.html";
            System.Gadget.onSettingsClosed = readSettings;
            System.Gadget.onShowSettings = Speichern;
            // System.Gadget.onSettingsClosed = ...;
            readSettings();
            document.getElementById("status").innerHTML = '<img src="status_5.png">';
            if (System.Gadget.Settings.read("units") == 2) {
                document.getElementById("topspd").firstChild.nodeValue = (topspd * 0.621371).toFixed(2).toString().replace(",", ".") + " MPH";
            } else {
                document.getElementById("topspd").firstChild.nodeValue = topspd.toFixed(2).toString().replace(".", ",") + " km/h";
            }
            document.onkeyup = updateval;
            window.setInterval("document.fireEvent('onkeyup')", 100);
        }

    </script>
</head>
<body onLoad="init()" id="bodyid">
<g:background id="baa" src="back_1.png"/>
<div id="z1_label">total:</div>
<div id="wert">0,00 m</div>

<div id="z2_label">today:</div>
<div id="spd">0,00 m</div>

<div id="z3_label">speed:</div>
<div id="topspd">0,00 km/h</div>

<div id="bild"><a href="https://www.mousometer.de" onFocus="if(document.all) this.blur()">mousometer.de</a></div>
<div id="status" onClick="showopt();" onMouseOver="style.cursor='hand'">&nbsp;</div>

</body>
</html>